import { Entity, Column, Index } from 'typeorm';
import { BaseEntity } from '../../../common/entity/base.entity';

export enum SecurityEventType {
  // 认证相关
  AUTH_FAILED = 'auth_failed',
  AUTH_SUCCESS = 'auth_success',
  TOKEN_EXPIRED = 'token_expired',
  TOKEN_INVALID = 'token_invalid',
  UNAUTHORIZED_ACCESS = 'unauthorized_access',
  
  // 权限相关
  PERMISSION_DENIED = 'permission_denied',
  ROLE_VIOLATION = 'role_violation',
  
  // 异常相关
  VALIDATION_ERROR = 'validation_error',
  BUSINESS_ERROR = 'business_error',
  SYSTEM_ERROR = 'system_error',
  DATABASE_ERROR = 'database_error',
  
  // 安全威胁
  SQL_INJECTION_ATTEMPT = 'sql_injection_attempt',
  XSS_ATTEMPT = 'xss_attempt',
  CSRF_ATTEMPT = 'csrf_attempt',
  BRUTE_FORCE_ATTEMPT = 'brute_force_attempt',
  RATE_LIMIT_EXCEEDED = 'rate_limit_exceeded',
  
  // 其他
  SUSPICIOUS_ACTIVITY = 'suspicious_activity',
  OTHER = 'other'
}

export enum SecurityLevel {
  LOW = 'low',
  MEDIUM = 'medium',
  HIGH = 'high',
  CRITICAL = 'critical'
}

@Entity('security_logs')
@Index(['eventType'])
@Index(['securityLevel'])
@Index(['userId'])
@Index(['ipAddress'])
@Index(['createdAt'])
export class SecurityLog extends BaseEntity {
  @Column({
    name: 'event_type',
    type: 'varchar',
    length: 50,
    comment: '安全事件类型',
  })
  eventType: SecurityEventType;

  @Column({
    name: 'security_level',
    type: 'varchar',
    length: 20,
    default: SecurityLevel.MEDIUM,
    comment: '安全级别',
  })
  securityLevel: SecurityLevel;

  @Column({
    name: 'event_title',
    type: 'varchar',
    length: 200,
    comment: '事件标题',
  })
  eventTitle: string;

  @Column({
    name: 'event_description',
    type: 'text',
    comment: '事件详细描述',
  })
  eventDescription: string;

  @Column({
    name: 'user_id',
    type: 'varchar',
    length: 36,
    nullable: true,
    comment: '用户ID（如果涉及用户）',
  })
  userId?: string;

  @Column({
    name: 'username',
    type: 'varchar',
    length: 50,
    nullable: true,
    comment: '用户名（如果涉及用户）',
  })
  username?: string;

  @Column({
    name: 'ip_address',
    type: 'varchar',
    length: 45,
    nullable: true,
    comment: 'IP地址',
  })
  ipAddress?: string;

  @Column({
    name: 'user_agent',
    type: 'text',
    nullable: true,
    comment: '用户代理',
  })
  userAgent?: string;

  @Column({
    name: 'request_url',
    type: 'varchar',
    length: 500,
    nullable: true,
    comment: '请求URL',
  })
  requestUrl?: string;

  @Column({
    name: 'request_method',
    type: 'varchar',
    length: 10,
    nullable: true,
    comment: '请求方法',
  })
  requestMethod?: string;

  @Column({
    name: 'api_name',
    type: 'varchar',
    length: 100,
    nullable: true,
    comment: '接口名称',
  })
  apiName?: string;

  @Column({
    name: 'request_headers',
    type: 'json',
    nullable: true,
    comment: '请求头信息',
  })
  requestHeaders?: any;

  @Column({
    name: 'request_body',
    type: 'json',
    nullable: true,
    comment: '请求体信息',
  })
  requestBody?: any;

  @Column({
    name: 'request_params',
    type: 'json',
    nullable: true,
    comment: '请求参数',
  })
  requestParams?: any;

  @Column({
    name: 'error_code',
    type: 'varchar',
    length: 50,
    nullable: true,
    comment: '错误代码',
  })
  errorCode?: string;

  @Column({
    name: 'error_message',
    type: 'text',
    nullable: true,
    comment: '错误信息',
  })
  errorMessage?: string;

  @Column({
    name: 'stack_trace',
    type: 'text',
    nullable: true,
    comment: '堆栈跟踪',
  })
  stackTrace?: string;

  @Column({
    name: 'http_status_code',
    type: 'int',
    nullable: true,
    comment: 'HTTP状态码',
  })
  httpStatusCode?: number;

  @Column({
    name: 'response_data',
    type: 'json',
    nullable: true,
    comment: '响应数据',
  })
  responseData?: any;

  @Column({
    name: 'execution_time',
    type: 'int',
    nullable: true,
    default: 0,
    comment: '执行时间(ms)',
  })
  executionTime?: number;

  @Column({
    name: 'session_id',
    type: 'varchar',
    length: 100,
    nullable: true,
    comment: '会话ID',
  })
  sessionId?: string;

  @Column({
    name: 'referer',
    type: 'varchar',
    length: 500,
    nullable: true,
    comment: '来源页面',
  })
  referer?: string;

  @Column({
    name: 'country',
    type: 'varchar',
    length: 50,
    nullable: true,
    comment: '国家/地区',
  })
  country?: string;

  @Column({
    name: 'city',
    type: 'varchar',
    length: 50,
    nullable: true,
    comment: '城市',
  })
  city?: string;

  @Column({
    name: 'is_handled',
    type: 'boolean',
    default: false,
    comment: '是否已处理',
  })
  isHandled: boolean;

  @Column({
    name: 'handled_at',
    type: 'timestamp',
    nullable: true,
    comment: '处理时间',
  })
  handledAt?: Date;

  @Column({
    name: 'handled_by',
    type: 'varchar',
    length: 36,
    nullable: true,
    comment: '处理人ID',
  })
  handledBy?: string;

  @Column({
    name: 'handled_note',
    type: 'text',
    nullable: true,
    comment: '处理备注',
  })
  handledNote?: string;
}
